/*global module:false*/
module.exports = function(grunt) {
  // Project configuration.
  readOptionalJSON = function( filepath ) {
	var data = {};
	try {
		data = grunt.file.readJSON( filepath );
	} catch(e) {}
	return data;
  };
  var config = {
	filepath : "package.json",
	prefix   : "src/main/webapp"
  }
  ;
  processRelativePath = function(filepath , prefix, flag ) {
		var data = readOptionalJSON(filepath),
		pathArray = [],
		newPathArray = [];
		var csscontent = grunt.file.read( prefix + flag );
		var regexp = new RegExp("\\@import url\\(\"\/"+data.moduleName+"(.*?)\"\\);", "g");
		console.log( "============ css file =============" );
		while((result=regexp.exec(csscontent)) != null) {
			pathArray.push(result[1]);
			console.log( prefix + result[1] );
		}
		for ( var index=0 ; index < pathArray.length ; index++) {
			var newpath = pathArray[index].substring(0, pathArray[index]
					.lastIndexOf("/") + 1);
			var fileName = pathArray[index].substring(pathArray[index]
					.lastIndexOf("/") + 1, pathArray[index].lenth);
			var newFilePath = newpath + "tmp_" + fileName;
			newPathArray[index] = prefix+newFilePath;
			var content = grunt.file.read(prefix+pathArray[index], "utf8").toString();
			var newContent = content.replace(/url\(["']?(.*?)["']?\)/g, "url(" + "/" + data.moduleName + newpath+ "$1)");
			grunt.file.write(prefix+newFilePath, newContent);
		}
		return newPathArray;
	};

	getResource = function( filepath ,prefix,  flag )
	{
		  var data = readOptionalJSON(filepath);
		  var jscontent =  grunt.file.read( prefix + flag  );
		  var regexp = new RegExp("document\\.write\\(\"<script src='\/"+data.moduleName+"(.*?)' type='text\/javascript'><\/script>\"\\);", "g");
		  var sourceArr = ["<banner:meta.banner>"];
		  console.log( "============ js file =============" );
		  while((result=regexp.exec(jscontent)) != null) {
			  sourceArr.push(prefix + result[1]);
			  console.log( prefix + result[1] );
		  }
		  
		  return sourceArr;
	};
  
  grunt.initConfig({
				pkg : '<json:'+ config.filepath +'>',
				meta : {
					banner : '/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %> - '
							+ '<%= grunt.template.today("yyyy-mm-dd") %>\n'
							+ '<%= pkg.homepage ? "* " + pkg.homepage + "\n" : "" %>'
							+ '* Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author.name %>;'
							+ ' Licensed <%= _.pluck(pkg.licenses, "type").join(", ") %> */'
				},
				concat : {
					//js dist
					login_dist : {
						src : getResource( config.filepath , config.prefix ,"/js/login.min.js"),
						dest : "<%= pkg.prefix %>/js/login.min.js"
					}
					,
					register_dist : {
						src : getResource( config.filepath , config.prefix ,"/js/register.min.js"),
						dest : "<%= pkg.prefix %>/js/register.min.js"
					}
					,
					userinfo_dist : {
						src : getResource( config.filepath , config.prefix ,"/js/userinfo.min.js"),
						dest : "<%= pkg.prefix %>/js/userinfo.min.js"
					}
					,
					contact_dist : {
						src : getResource( config.filepath , config.prefix ,"/js/contact.min.js"),
						dest : "<%= pkg.prefix %>/js/contact.min.js"
					},
					device_dist : {
						src : getResource( config.filepath , config.prefix ,"/js/device.min.js"),
						dest : "<%= pkg.prefix %>/js/device.min.js"
					}
					,
					
					//css dist
					css_login_dist : {
						src : processRelativePath(config.filepath , config.prefix, "/css/login.min.css"),
						dest : "<%= pkg.prefix %>/css/login.min.css"
					}
					,
					css_register_dist : {
						src : processRelativePath(config.filepath , config.prefix,"/css/register.min.css"),
						dest : "<%= pkg.prefix %>/css/register.min.css"
					}
					,
					css_contact_dist : {
						src : processRelativePath(config.filepath , config.prefix,"/css/contact.min.css"),
						dest : "<%= pkg.prefix %>/css/contact.min.css"
					}
					,
					css_userinfo_dist : {
						src : processRelativePath(config.filepath , config.prefix,"/css/userinfo.min.css"),
						dest : "<%= pkg.prefix %>/css/userinfo.min.css"
					}
					,
					css_device_dist : {
						src : processRelativePath(config.filepath , config.prefix,"/css/device.min.css"),
						dest : "<%= pkg.prefix %>/css/device.min.css"
					}
				},
				min : {
					// min javascript
					login_dist : {
						src : [ '<banner:meta.banner>',
								'<config:concat.login_dist.dest>' ],
						dest : '<config:concat.login_dist.dest>'
					},
					register_dist : {
						src : [ '<banner:meta.banner>',
								'<config:concat.register_dist.dest>' ],
						dest : '<config:concat.register_dist.dest>'
					},
					contact_dist : {
						src : [ '<banner:meta.banner>',
								'<config:concat.contact_dist.dest>' ],
						dest : '<config:concat.contact_dist.dest>'
					},
					userinfo_dist : {
						src : [ '<banner:meta.banner>',
								'<config:concat.userinfo_dist.dest>' ],
						dest : '<config:concat.userinfo_dist.dest>'
					},
					device_dist : {
						src : [ '<banner:meta.banner>',
								'<config:concat.device_dist.dest>' ],
						dest : '<config:concat.device_dist.dest>'
					}
				},
				// min css
				cssmin : {
					min_login : {
						src : '<config:concat.css_login_dist.dest>',
						dest: '<config:concat.css_login_dist.dest>'
					}
					,
					min_register : {
						src : '<config:concat.css_register_dist.dest>',
						dest: '<config:concat.css_register_dist.dest>'
					}
					,
					min_userinfo : {
						src : '<config:concat.css_userinfo_dist.dest>',
						dest: '<config:concat.css_userinfo_dist.dest>'
					}
					,
					min_contact : {
						src : '<config:concat.css_contact_dist.dest>',
						dest: '<config:concat.css_contact_dist.dest>'
					}
					,
					min_device : {
						src : '<config:concat.css_device_dist.dest>',
						dest: '<config:concat.css_device_dist.dest>'
					}
				},
				uglify : {}
			});
   grunt.loadNpmTasks('grunt-css');
   grunt.registerTask('production', 'concat min cssmin');
};
